package com.dbz.drivingTools.utils;

import android.util.Log;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class SerialUtil {

    static {
        System.loadLibrary("native_lib");
    }

    private static final String TAG = "SerialUtil";

    public static final String[] BaudRate = {"9600", "19200", "38400", "57600", "115200"};

    private FileDescriptor fd;
    private FileInputStream fis;
    private FileOutputStream fos;

    private static List<String> getSerialDrivers() {
        List<String> drivers = new ArrayList<>();

        try {
            String line;
            LineNumberReader reader = new LineNumberReader(new FileReader("/proc/tty/drivers"));
            while ((line = reader.readLine()) != null) {
                String[] list = line.split(" +");
                if (list[list.length - 1].equals("serial")) {
                    Log.d(TAG, "Found new driver: " + list[list.length - 4]);
                    drivers.add(list[list.length - 4]);
                }
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return drivers;
    }

    public static List<String> getSerialDevices() {
        List<String> drivers = getSerialDrivers();
        List<String> devices = new ArrayList<>();

        File[] devFiles = new File("/dev").listFiles();
        if (devFiles != null) {
            for (String driver : drivers) {
                for (File file : devFiles) {
                    String device = file.getAbsolutePath();
                    if (device.startsWith(driver)) {
                        Log.d(TAG, "Found new device: " + device);
                        devices.add(device);
                    }
                }
            }
        }

        return devices;
    }

    public boolean openDevice(String deviceName, int baudRate) {
        if (deviceName == null || baudRate < 0)
            return false;

        fd = open(deviceName, baudRate, 0);
        if (fd != null) {
            fis = new FileInputStream(fd);
            fos = new FileOutputStream(fd);
            return true;
        }
        return false;
    }

    public void closeDevice() {
        if (fd != null) {
            try {
                fos.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            close(fd);
            fd = null;
        }
    }

    public InputStream getInputStream() {
        return fis;
    }

    public OutputStream getOutputStream() {
        return fos;
    }

    private native FileDescriptor open(String path, int baudRate, int flags);

    private native boolean close(FileDescriptor fd);

}
